
var deleteUrl = "DeleteGiftWatchById";
let startTime = "";
let endTime = "";
layui.config({
    version: '1510164124181'
}).use(['laypage', 'layer', 'table','form'], function () {
    table = layui.table
        , laypage = layui.laypage
        , layer = layui.layer
        , form = layui.form;

    var active = {
        getAddData: function () { //添加
            AddInfo("");
        }
        , getCheckData: function () { //批量删除
            var checkStatus = table.checkStatus('tableInfoId')
                , data = checkStatus.data;
            var dataId = "";
            for (var i = 0; i < data.length; i++) {
                dataId += data[i].id + ",";
            }
            if (dataId.length > 0) {
                dataId = dataId.substring(0, dataId.length - 1);
                DeleteAjaxHttp(dataId, deleteUrl);
            } else {
                top.layer.msg("请选择你要删除的数据!!");
            }

        }
    };

    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //监听操作
    form.on('switch(enableDemo)', function (obj) {
        var attr = $(this).attr("data-id");
        var info = $(this).attr("data-info");

        editInfo(attr,"enable",1-Number(info));
    });
    //监听搜索操作
    form.on('submit(formSubSearch)', function (obj) {
        pageNo = 1;
        inituserdata();
    });
    //监听工具条
    table.on('tool(tableInfo)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            DeleteAjaxHttp(data.id, deleteUrl);
        } else if (obj.event === 'edit') {
            AddInfo(data.id);
        }
    });

    //监听单元格编辑
    table.on('edit(tableInfo)', function (obj) {
        let value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        layer.confirm("您确定将此条信息修改为:" + value + "?", {icon: 3, title: '提示信息'}, function (index) {
            index = top.layer.msg('提交中，请稍候', {icon: 16, time: false, shade: 0.8});
            editInfo(data.id, field, value);
        });
    });

    inituserdata();
});
function onloadInfo() {
    inituserdata();
}
function AddInfo(dataId) {
    var tempInfo = "?page="+pageNo;
    if (dataId != null && dataId != "" && dataId != undefined) {
        tempInfo += "&GoldGiftId=" + dataId;
    }
    layer.open({
        type: 2,
        title:['金币兑换商品设置','color:red;font;font-weight: 600;'],
        area: ['80%', '80%'],
        fixed: false, //不固定
        maxmin: true,
        content: 'AddGoldGift.html' + tempInfo
    });
}
function inituserdata() {
    var index = top.layer.msg('加载中，请稍候', {icon: 16, time: false, shade: 0.8});
    let SelectInfo = $("#isseinfo option:selected").val();
    let InputInfo = $("#SearchInfo").val();
    $.ajax({
        url: httpurl + "/callGiftWatch",
        cache: false,
        data: {
            "page": pageNo,
            "pagesize": pageRecords,
            "enable": SelectInfo,
            "giftname": InputInfo,
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        success: function (data) {
            let dataInfo = [];
            if (data.rtnCode == 200) {
                dataInfo = data.data.list;
                totalRecords = data.data.total;
            } else {
                totalRecords = 0;
            }
            table.render({
                elem: '#tableInfo'
                , data: dataInfo
                , id: "tableInfoId"
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'id', align: "center", width: 80, title: 'ID', sort: true}
                    , {field: 'giftname', align: "center", width: 100, title: '名称'}
                    , {field: 'image', align: "center", width: 100, title: '图片', sort: true,
                        templet: function (res) {
                            return (res.image==""?"":'<img width="80" src="' + visitimgurl + '/' + res.image + '"/>');
                        }}
                    ,{field: 'isground', align: "center", title: '是否启用', templet: '#enable ', unresize: true}
                    , {field: 'number', align: "center",edit: 'text',  title: '数量'}
                    , {field: 'goldnum', align: "center",edit: 'text',  title: '需要金币数量'}
                    , {field: 'sort', align: "center",edit: 'text',  title: '排序'}
                    , {field: 'starttime', align: "center", width:300, title: '活动时间',templet: function (res) {
                            return res.starttime+" - "+res.endtime;
                        }}
                    , {field: 'del', align: "center", width: 100, title: '操作', templet: '#barDemo', unresize: true}
                ]]
            });

            top.layer.closeAll();
            page();
        }
    });
}

function editInfo(id, field, value) {
    var data = {};
    data["id"] = id;
    data[field] = value;
    // data["username"] = username;
    $.ajax({
        type: "POST",
        url: httpurl + "/updateGiftWatchById",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: JSON.stringify(data),
        success: function (data) {
            top.layer.closeAll();
            top.layer.alert(data.msg);
        }
    });
}

//分页
function page() {
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        , count: totalRecords //数据总数，从服务端得到
        , layout: ['count', 'prev', 'page', 'next', 'refresh', 'skip']
        , curr: pageNo //获取hash值为fenye的当前页
        , hash: 'fenye' //自定义hash值
        , limit: pageRecords
        , jump: function (obj, first) {
            //首次不执行
            if (!first) {
                pageNo = obj.curr;// 当前页数;
                pageRecords = obj.limit;// 当前页面记录数
                inituserdata();
            }
        }
    });
}
